/**
 * Created by shaoting on 16/10/13.
 * 点评界面
 */
import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    ListView,
    Image,
    TouchableOpacity,
    Platform,
    Dimensions,
    AsyncStorage
} from 'react-native';
var screenWidth = Dimensions.get('window').width; //屏宽
//var Comment = require('../Mine/Comment.json');
var CommentsView = React.createClass({
    getDefaultProps(){
        //回调函数
        return{
            selectCellAction:null
        }
    },
    componentDidMount(){
        var token = '';
        try {
            AsyncStorage.getItem(
                'token',
                (error,result)=>{
                    if (error){
                        alert('取值失败:'+error);
                    }else{
                        // alert('取值成功:'+result);
                        this.token = result
                    }
                }
            )
        }catch(error){
            alert('失败'+error);
        }
        let formData = new FormData();
        formData.append("userId", '');
        formData.append("page", '0');
        formData.append("size", '5');
        var fetchOptions = {
            method: 'POST',
            headers: {
                'token': this.token
            },
            // body:formData,
        };

        let url = 'http://www.pintasty.cn/user/listcomment';
        fetch(url, fetchOptions)
            .then((response) => response.json())
            .then((json) => {
                console.log(json)
                if(json.status == 200){
                    this.setState({
                        dataSource:this.state.dataSource.cloneWithRows(json.data),
                    })
                }else  if (json.status == 301){
                    alert('没有token')
                }else {
                    alert('访问服务器失败')
                }
            }) .done();
    },
    getInitialState(){
        return{
            dataSource: new ListView.DataSource({rowHasChanged: (row1, row2) => row1 !== row2})
        }
    },
    render() {
        return (
            <ListView
                style={styles.container}
                bounces={false}
                dataSource={this.state.dataSource} //数据源
                renderRow={this.renderRow}
            />
        );
    },
    renderRow(rowData,sectionID,rowID,highlightRow){
        return(
            <TouchableOpacity onPress={()=>this.selectCellAction(rowData)}>
                <View>
                    <View style={{flexDirection:'row',marginTop:5}}>
                        <View>
                            <Image source={{uri:rowData.comment_user_photo}} style={{marginLeft:20,width:50,height:50,borderRadius:25}}/>
                        </View>
                        <View style={{justifyContent:'space-between'}}>
                            <Text style={{marginTop:3,marginLeft:10,fontSize:12,color:'red'}}>{rowData.comment_user_name}</Text>
                            <Text style={{marginLeft:10,fontSize:15}}>{rowData.comment_user_sign}</Text>
                            <Text style={{marginLeft:10,fontSize:10}}>{rowData.comment_create_time}</Text>
                        </View>
                    </View>
                    <View>
                        <Text style={{marginLeft:20,marginTop:10,fontSize:19}}>{rowData.comment_content}</Text>
                    </View>
                    <View style={{marginTop:10}}>
                        <Image source={{uri:rowData.product_img}} style={{marginLeft:20,width:screenWidth-40,height:screenWidth-40}}/>
                    </View>
                    <View style={{marginTop:5,borderBottomWidth:3,borderBottomColor:'#e8e8e8'}}/>
                </View>
            </TouchableOpacity>
        );
    },
    selectCellAction(rowData){
        // console.log(rowData)
        // 判断
        // if (this.props.selectCellAction == null) return;
        this.props.selectCellAction(rowData)
        // 使用global传值方法
        global.uploadProductName = rowData.comment_user_name
    }
});


var styles = StyleSheet.create({
    container:{
        width:screenWidth,
        flex:1,
        backgroundColor:'white'
    }
});

module.exports = CommentsView;